package com.echoclient;

import com.cn.model.Request;
import com.cn.module.fuben.request.FightRequest;
import com.cn.mycodc.MyRequestEncoder;
import com.cn.mycodc.MyResponseDecoder;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;
import java.util.Scanner;

/**
 * 代码清单 2-4 客户端的主类
 *
 * @author <a href="mailto:norman.maurer@gmail.com">Norman Maurer</a>
 */
public class EchoClient {
    private final String host;
    private final int port;

    public EchoClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public void start()
        throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            //创建 Bootstrap
            Bootstrap b = new Bootstrap();
            //指定 EventLoopGroup 以处理客户端事件；需要适用于 NIO 的实现
            b.group(group)
                //适用于 NIO 传输的Channel 类型
                .channel(NioSocketChannel.class)
                //设置服务器的InetSocketAddress
                .remoteAddress(new InetSocketAddress(host, port))
                //在创建Channel时，向 ChannelPipeline中添加一个 EchoClientHandler实例
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch)
                        throws Exception {
                        ch.pipeline().addLast("decoder",new MyResponseDecoder());
                        ch.pipeline().addLast("encoder",new MyRequestEncoder());
                        ch.pipeline().addLast("handler",new EchoClientHandler());

                    }
                });
            //连接到远程节点，阻塞等待直到连接完成
            ChannelFuture f = b.connect().sync();
            //调用输入
            Channel channel = f.channel();
            readInputAndSendMsg(channel);
            //阻塞，直到Channel 关闭
            f.channel().closeFuture().sync();
        } finally {
            //关闭线程池并且释放所有的资源
            group.shutdownGracefully().sync();
        }
    }

    public static void main(String[] args) throws Exception {


        final String host = "localhost";
        final int port = 10101;
        new EchoClient(host, port).start();
    }

    private static void readInputAndSendMsg(Channel channel) {
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("请输入");
            int fubenId = Integer.parseInt(scanner.nextLine());
            int count = Integer.parseInt(scanner.nextLine());

            FightRequest fightRequest = new FightRequest();
            fightRequest.setFubenId(fubenId);
            fightRequest.setCount(count);

            Request request = new Request();
            request.setModule((short) 1);
            request.setCmd((short) 1);
            request.setData(fightRequest.getBytes());
            //发送请求
            channel.writeAndFlush(request);
        }
    }
}

